package com.gl.emms.whale.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.DialogFactory;

/**阀门强制控制界面类
 * @author ganlinux
 *
 */
public class ValveControlActivity extends WhaleMonitorActivity implements OnClickListener{
	private TextView roomText;//房间显示控件
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_valvecontrol);
		initViews();
	}
	/**
	 * @time 2014年11月12日上午10:49:50
	 * @return void
	 * @user ganlinux
	 */
	public void initViews() {
		//设置返回按钮
		Button titleBackBtn = (Button)findViewById(R.id.TitleBackBtn);  
		titleBackBtn.setOnClickListener(this);
		//房间显示
		roomText = (TextView) findViewById(R.id.roomtext);
		roomText.setText(MyApplication.getUserData().getMainroom());
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.TitleBackBtn://返回
			new KeyEvent(KeyEvent.ACTION_DOWN,  KeyEvent.KEYCODE_BACK);
			finish();
			break;

		default:
			break;
		}
	}
	/**
	 * 选择控制模式
	 * @time 2014年11月12日上午10:56:28
	 * @return void
	 * @param v
	 * @user ganlinux
	 */
	public void change_controlmode(View v) {
		CharSequence[] items = { getText(R.string.control_on), getText(R.string.control_off)};
		controlModeChooseItem(items);
	}

	/**
	 * 控制模式选择
	 * @time 2014年11月12日上午10:58:53
	 * @return void
	 * @param items
	 * @user ganlinux
	 */
	public void controlModeChooseItem(CharSequence[] items) {
		AlertDialog imageDialog = new AlertDialog.Builder(this)
		.setTitle(getText(R.string.control_alert_title))
		.setIcon(android.R.drawable.btn_star)
		.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				String roomStr = MyApplication.getUserData().getMainroom(); 
				//控制开
				if (item == 0) {
					EMMSPushManager.requestForceControl(ValveControlActivity.this,MyApplication.getUserData().getIdIP(),roomStr,Constant.CONTROL_ON);
				}
				//控制关 
				else if (item == 1) {
					EMMSPushManager.requestForceControl(ValveControlActivity.this,MyApplication.getUserData().getIdIP(),roomStr,Constant.CONTROL_OFF);
				}
			}
		}).create();
		imageDialog.show();
	}
	@Override
	public void onReplyReceived(final ReplyBody reply) {

		if (reply.getType().equals(Constant.TYPE_FORCECONTROL_RESPONSE)) {
			if(reply.get(Constant.CODE_FORCECONTROL).equals(Constant.FORCECONTROL_SUCCESS))
			{
				DialogFactory.ToastDialog(ValveControlActivity.this,getString(R.string.control_result),
						getString(R.string.control_ok));
			}else {
				DialogFactory.ToastDialog(ValveControlActivity.this, getString(R.string.control_result),
						getString(R.string.control_fail));
			}

		}
	}
	
}